/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import MyThread.BCMCReadThread;
import MyThread.BCMCWritehread;
import com.barco.ledApi.BcmCDev;
import com.barco.ledApi.LedConfig;
import com.ideal.Interface.MainConfig;
import com.ideal.Interface.DevProcess;
import com.ideal.Interface.HttpSend;
import com.ideal.json.Employee;
import com.ideal.log.Log;
import com.sensor.Broadcast;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.json.JSONObject;

/**
 *
 * @author Huyu
 */
public class Main {

    /**
     * 构造Json数据
     *
     * @return
     * @throws JSONException
     */
    public static String BuildJson() throws JSONException, IllegalArgumentException, IllegalAccessException {

        // 下面构造两个map、一个list和一个Employee对象
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("name", "Alexia");
        map1.put("sex", "female");
        map1.put("age", "23");

        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("name", "Edward");
        map2.put("sex", "male");
        map2.put("age", "24");

        Map<String, Integer> map3 = new HashMap<String, Integer>();
        map3.put("sex", 1);
        map3.put("age", 2);

        List<Map> list = new ArrayList<Map>();
        list.add(map1);
        list.add(map2);
        list.add(map3);

        Employee employee = new Employee();
        employee.setName("wjl");
        employee.setSex("female");
        employee.setAge(24);

        // 将Map转换为JSONArray数据
        JSONArray ja = new JSONArray();
        ja.put(list);

        System.out.println("JSONArray对象数据格式：");
        System.out.println(ja.toString());

        // 将Javabean转换为Json数据（需要Map中转）
        /// JSONObject jo1 = JsonHelper.toJSON(employee);
//        System.out.println("\n仅含Employee对象的Json数据格式：");
//        System.out.println(jo1.toString());
        // 构造Json数据，包括一个map和一个含Employee对象的Json数据
        // JSON格式数据解析对象
        JSONObject jo = new JSONObject();
        jo.put("map", list);
        //jo.put("employee", jo1);
        System.out.println("\n最终构造的JSON数据格式：");
        System.out.println(jo.toString());
        // return jo.toString();
        return "";
    }

    /**
     * 解析Json数据
     *
     * @param jsonString Json数据字符串
     * @throws JSONException
     * @throws ParseException
     */
    public static void ParseJson(String jsonString) {

        try {
            JSONObject jo = new JSONObject(jsonString);
            JSONObject resObj = jo.getJSONObject("resource");
            String resName = resObj.getString("name");

            JSONArray paraArray = jo.getJSONArray("params");
            int paralen = paraArray.length();

            List<Map> list = new ArrayList<Map>();
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < paralen; i++) {
                JSONObject paraObj = paraArray.getJSONObject(i);
                //String value = paraObj..getString("value1");
//                String Name = paraObj.getString("name");
//                String state = paraObj.getString("state");
                Object obj = paraObj.get("name");
                Object obj1 = paraObj.get("state");
                Object obj2 = paraObj.get("seq");
                Object obj3 = "";

                if (obj1.equals("STATE_VALID")) {
                    obj3 = paraObj.get("value");

                }
                String NameStr = obj.toString();
                NameStr = NameStr.replaceAll("/", "_");
                map.put(NameStr, obj3);
                //String seq = paraObj.getString("seq");
                String a1 = "";

                //Integer.to
            }
            list.add(map);

            // System.out.println(ja.toString());
            JSONObject jobj = new JSONObject();
            jobj.put("projectorList", list);
            System.out.println(jobj.toString());
            String a = "";
        } catch (JSONException ex) {
            System.out.println("" + ex.getMessage());
        }

        //JSONArray ja = jo.getJSONArray("resource");
        //System.out.println("\n将Json数据解析为Map：");
        // int len = ja.length();
//        System.out.println("name: " + ja.getJSONObject(0).getString("name")
//                + " sex: " + ja.getJSONObject(0).getString("sex") + " age: "
//                + ja.getJSONObject(0).getInt("age"));
//        String jsonStr = jo.getString("employee");
//        
//        Employee emp = new Employee();
//        JsonHelper.toJavaBean(emp, jsonStr);
//
//        System.out.println("\n将Json数据解析为Employee对象：");
//        System.out.println("name: " + emp.getName() + " sex: " + emp.getSex()
//                + " age: " + emp.getAge());
    }

    public static void main(String[] args) throws Exception {
//        ArrayList<String> test = new ArrayList<String>();
//        LedConfig.getLidList(test);
//        test.add("1231213241s");
//        ArrayList<String> test1 = new ArrayList<String>();
//        LedConfig.getLidList(test1);
//        LedConfig.AddIp("123456");
//        ArrayList<String> test2 = new ArrayList<String>();
//        LedConfig.getLidList(test2);

        String Curdir = System.getProperty("user.dir");//当前路径
        String Confdir = Curdir + "/conf/";//配置文件路径

        Log.init(Confdir);//初始化log4j
        MainConfig.init(Confdir);//初始化配置问及
//
//        //BcmDev bcm = new BcmDev();//bcm设备
////        SensorDev sensor = new SensorDev();//传感器设备       
        BcmCDev bcmc = new BcmCDev();//bcmc设备
////      List<Map> objList = bcmc.getDevInfo();
//        DevProcess pro = new DevProcess();
//        pro.RegisterDev(bcmc);//注册bcmc设备
////       // pro.RegisterDev(sensor);
//        List<Map> objList = pro.start();//获取返回对象
//        if(0 == objList.size()){
//            return;
//        }

        //测试数据组装
//        List<Map> objList = new ArrayList<Map>();
//        Map<String, String> objInfo1 = new HashMap<String, String>();
//        Map<String, String> objInfo2 = new HashMap<String, String>();
//        objInfo1.put("macaddress", "00:1A:23:4d:44");
//        objInfo1.put("temperature", "26");
//        objInfo1.put("humidity", "36");
//        objInfo1.put("ip", "192.168.1.56");
//        objInfo2.put("macaddress", "00:1B:23:4d:44");
//        objInfo2.put("temperature", "29");
//        objInfo2.put("humidity", "33");
//        objInfo2.put("ip", "192.168.1.58");
//        objList.add(objInfo1);
//        objList.add(objInfo2);
//        JSONObject sensorJs = new JSONObject();
//        sensorJs.put("sensor", objList);
//        System.out.println("生成数据为");
//        System.out.println(sensorJs.toString());
       // JSONObject jobj = new JSONObject();
        //jobj.put("projectorList", objList);
//        System.out.println("生成数据为");
//        System.out.println(jobj.toString());
//        String urlStr = "http://192.168.1.104/idea80/data/bcm";
//        HttpSend httpsend = new HttpSend(urlStr);
//        httpsend.sendMessage(sensorJs.toString());
        //本地数据测试
//        File file = new File("D:\\BlueStart\\40001\\2.txt");
//        BufferedReader reader = null;
//        String jsonStr = "";
//        try {
//            // System.out.println("以行为单位读取文件内容，一次读一整行：");
//            reader = new BufferedReader(new FileReader(file));
//            String tempString = null;
//            int line = 1;
//            // 一次读入一行，直到读入null为文件结束
//            while ((tempString = reader.readLine()) != null) {
//                // 显示行号
//                jsonStr = jsonStr + tempString;
//                //System.out.println("line " + line + ": " + tempString);
//                line++;
//            }
//            reader.close();
//        } catch (IOException e) {
//
//        } 
//        
//        Map<String, Object> map = new HashMap<String, Object>();
//        bcmc.ParseJson(map, jsonStr);
//        List<Map> list = new ArrayList<Map>();
//        list.add(map);
//        JSONObject jsonObj = new JSONObject();
//        jsonObj.put("projectorList", list);
//        System.out.println("生成数据为");
//        System.out.println(jsonObj.toString());
//        
//        String urlStr = "http://192.168.1.106/data/bcm";
//        HttpSend httpsend = new HttpSend(urlStr);
//        httpsend.sendMessage(jsonObj.toString());
        
        
        //传感器测试
        int udp_port = 9000;
        Broadcast a = new Broadcast(udp_port);
        a.AddNewSensor("192.168.1.54", 10050);
        List<Map> list = a.getDevInfo();
        if(list.size() == 0){
            return;
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("sensorList", list);
        System.out.println("生成数据为");
        System.out.println(jsonObj.toString());
        
        
//        String urlStr = "http://192.168.1.211/data/bcm";
//        HttpSend httpsend = new HttpSend(urlStr);
//        httpsend.sendMessage(jsonObj.toString());
    }
}
